package com.baosight.risk.web.riaa;

import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baosight.risk.api.riaa.RiaaQuesInquiryService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.riaa.entity.RiaaQuesInquiry;
import com.baosight.risk.service.riaa.vo.ChangeSubmitUserVO;
import com.baosight.risk.service.riaa.vo.HandOverUserVO;
import com.baosight.risk.service.riaa.vo.RiaaQuesInquiryVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 类 名: RiaaQuesInquiryController
 * 描 述: 问卷制作
 * 作 者: 黎秋荣
 * 创 建：2019年06月05日
 * 版 本：v2.2.0
 * 历 史: (版本) 作者 时间 注释
 */
@Slf4j
@RestController
@RequestMapping("/riaa/ques/inquiry")
@Api(value = "问卷制作", description = "问卷制作", produces = MediaType.APPLICATION_JSON)
public class RiaaQuesInquiryController extends BaseController {

    @Autowired
    private RiaaQuesInquiryService riaaQuesImquiryService;


    @RequestMapping("selectList")
    @ApiOperation("问卷列表查询")
    public AjaxResult selectList(RiaaQuesInquiry inquiry) {
        log.info("问卷列表查询：" + inquiry.toString());
        inquiry.setSendCompCode(ShiroUtils.getUser().getManageCompCode());
        startPage();
        return getResultPage(riaaQuesImquiryService.selectList(inquiry));
    }

    @GetMapping("getById")
    @ApiOperation("问卷根据ID查询")
    public AjaxResult getById(@RequestParam("id") String id) {
        return success(riaaQuesImquiryService.selectById(id));
    }

    @PostMapping("save")
    @Log(title = "问卷制作添加", businessType = BusinessType.INSERT)
    @ApiOperation("问卷制作添加")
    public AjaxResult save(@RequestBody RiaaQuesInquiryVO inquiryVO) {
//        if (CollectionUtils.isEmpty(inquiryVO.getCompList())) {
//            log.error("下发单位信息为空，请至少选择一条信息！");
//            return error("下发单位信息为空，请至少选择一条信息！");
//        }
        // 调用新增接口
        log.info("问卷制作新增：" + inquiryVO.toString());
        return success(riaaQuesImquiryService.saveInquiry(inquiryVO));

    }

    @GetMapping("sendInquiry")
    @Log(title = "问卷制作下发")
    @ApiOperation("问卷制作下发")
    public AjaxResult sendInquiry(@RequestParam("id") String id,@RequestParam("flag") String flag) throws Exception {
//        RiaaQuesInquiry inquiry = riaaQuesImquiryService.get(id);
//        if (null == inquiry) {
//            log.error("数据有误！");
//            return error("数据有误！");
//        }
        riaaQuesImquiryService.saveSendInquiry(id,flag);
        return success();
    }


    @PostMapping("delete")
    @Log(title = "问卷删除", businessType = BusinessType.DELETE)
    @ApiOperation("问卷删除")
    public AjaxResult delete(String id) {
        RiaaQuesInquiry inquiry = riaaQuesImquiryService.get(id);
        if (null == inquiry) {
            log.error("数据有误！");
            return error("数据有误！");
        }
        if ("10".equals(inquiry.getStatus())) {
            log.error("已下发状态不可以删除！");
            return error("已下发状态不可以删除！");
        }
        return toAjax(riaaQuesImquiryService.deleteInquiry(id));
    }

    @PostMapping("queryCompUser")
    @ApiOperation("根据单位找人")
    public AjaxResult queryCompUser(@RequestBody Map<String,Object> param) throws Exception {
        log.info("参数:" + param.toString());
        return  success(riaaQuesImquiryService.queryCompUser(param));
    }

    @PostMapping("deleteType")
    @Log(title = "问卷选项删除", businessType = BusinessType.DELETE)
    @ApiOperation("问卷选项删除")
    public AjaxResult deleteGoal(@RequestBody MultipleUpdateStatus updateStatus) {
        List<String> ids = updateStatus.getIds();
        if (StringUtils.isEmpty(ids)) {
            return error("数据有误！");
        }
        riaaQuesImquiryService.deleteType(ids);
        return success();
    }

    @PostMapping("changeSubmitUser")
    @Log(title = "问卷制作移交管理员", businessType = BusinessType.INSERT)
    @ApiOperation("问卷制作移交管理员")
    public AjaxResult updateSubmitUser(@RequestBody ChangeSubmitUserVO param) throws Exception {
        riaaQuesImquiryService.updateSubmitUser(param.getEmployee(), param.getRiquInquiryComp());
        return success();
    }

    @PostMapping("handOverUser")
    @Log(title = "问卷移交", businessType = BusinessType.INSERT)
    @ApiOperation("问卷移交")
    public AjaxResult handOverUser(@RequestBody HandOverUserVO param) throws Exception {
        riaaQuesImquiryService.handOverUser(param.getXtzyUser(), param.getRiquInquiryComp());
        return success();
    }


}
